<?php
// PXFInstance::breakPoint($current_categories);

class ControllerAdmin extends PXFControllerAdmin
{
	static $tablinks = null;

	public function __construct()
	{ 
		self::$tablinks = array(
			array('controller' => 'ajax_categories', 'action' => 'index',  'title' => 'Listado de Categor&iacute;as'),
		);
		parent::__construct();
		$this->tpl->assign('tablinks',self::$tablinks);
	}

	public function index()
	{
		PXFConfigAdmin::$site['page_title'] = 'Categor&iacute;as';
		$this->tpl->assign('site',PXFConfigAdmin::$site);
		$this->tpl->display('ajax_categories.list.tpl');
	}

	public function categories()
	{
		$id = PXFRequest::reqGetOr404('id'); 
		$mod_item = PXFModel::getModel('item');
		$mod_categories = PXFModel::getModel('category');
		$categories     = $mod_categories->findAll();
		$item           = $mod_item->find($id);
		PXFRequest::notNullOr404($item); 
		$current_categories = $item->category();

		$result = array();	
		foreach($categories as $cat)
		{
			$check = false;	
			foreach($current_categories as $ccat)
			{
				if($ccat->id == $cat->id)	{ $check = true; break; }
			}
			$result[] = array('title' => $cat->title,'id' => $cat->id, 'checked' => $check);
		}
		echo pxf_json_encode($result);
	}

	public function category_add()
	{
		$itm_id = PXFRequest::reqGetOr404('itm_id'); 
		$cat_id = PXFRequest::reqGetOr404('cat_id'); 
		$mod_item       = PXFModel::getModel('item');
		$mod_categories = PXFModel::getModel('category');
		$item = $mod_item->find($itm_id);
		$category = $mod_categories->find($cat_id);
		$item->category->add($category);
		echo pxf_json_encode(array('msg' => sprintf('Se ha agregado la categoria %s.',$category->title)));
	}
	
	public function category_del()
	{
		$itm_id = PXFRequest::reqGetOr404('itm_id'); 
		$cat_id = PXFRequest::reqGetOr404('cat_id'); 
		$mod_item       = PXFModel::getModel('item');
		$mod_categories = PXFModel::getModel('category');
		$item = $mod_item->find($itm_id);
		$category = $mod_categories->find($cat_id);
		$item->category->remove($category);
		echo pxf_json_encode(array('msg' => sprintf('El item ha sido removido de la categoria %s.',$category->title)));
	}

	public function category_edit()
	{
		$cat_id = PXFRequest::reqGetOr404('cat_id'); 
		$title  = PXFRequest::reqGetOrDefault('title',''); 
		$mod_categories = PXFModel::getModel('category');
		$category = $mod_categories->find($cat_id);
		$title2 = rawurldecode($title);
		$category->title = $title2;
		$category->save();
		echo pxf_json_encode(array('msg' => sprintf('La categoria %s ha sido renombrada correctamente.',$title2)));
	}

	public function category_create()
	{
		$title  = PXFRequest::reqGetOrDefault('title',''); 
		$mod_categories = PXFModel::getModel('category');
		$category = $mod_categories->getNew();
		$category->title = $title;
		$category->save();
		echo pxf_json_encode(array('msg' => 'La categoria ha sido creada correctamente.'));
	}

	public function categories_get_all()
	{
		$mod_categories = PXFModel::getModel('category');
		$categories = $mod_categories->findAll();

		$result = array();
		foreach($categories as $cat)
		{
			$result[] = array('title' => $cat->title,'id' => $cat->id);
		}
		echo pxf_json_encode($result);
	}

	public function categories_delete()
	{
		$id = PXFRequest::reqGetOr404('id'); 
		$mod_categories = PXFModel::getModel('category');
		$category = $mod_categories->find($id);
		$title = $category->title;
		$items = $category->item();
		foreach($items as $item)
		{
			$category->remove($item);
		}	
		$category->delete();
		echo pxf_json_encode(array('msg' => sprintf('La categoria %s ha sido eliminada.',$title)));

	}
}
